package edu.cth.speedapp.student;

import edu.cth.speedapp.R;
import edu.cth.speedapp.asynctasks.HTTPAsyncTask;
import edu.cth.speedapp.asynctasks.ITaskCallback;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ImageView;
import android.widget.Toast;

public class StudentTempo extends Fragment implements ITaskCallback, OnClickListener{
	private String roomName;
	private String tempo;
	
	private String deviceUuid;
	
	public StudentTempo(String roomName, String deviceUuid) {
		super();
		this.deviceUuid = deviceUuid;
		this.roomName = roomName;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		inflater.inflate(R.layout.fragment_student_tempo , container);
		
		
		
		getActivity().findViewById(R.id.stopButton).setOnClickListener(this);
		getActivity().findViewById(R.id.okButton).setOnClickListener(this);
		getActivity().findViewById(R.id.hardButton).setOnClickListener(this);
		getActivity().findViewById(R.id.easyButton).setOnClickListener(this);
		
		
		
		return super.onCreateView(inflater, container, savedInstanceState);
		
	}
	
	//From student in room activity
	public void onClickStop(View v) {
    	tempo = "stop";
    	HTTPAsyncTask t = new HTTPAsyncTask(this);
    	t.execute(getString(R.string.baseURL) + "/changeTempo.php?tempo="+tempo+"&roomName=" + roomName + "&studentID=" + deviceUuid);
    	ImageView imageView = (ImageView) getActivity().findViewById(R.id.bgImageView);
    	imageView.setImageResource(R.drawable.stop);
    	
    }
    
    public void onClickHard(View v) {
    	tempo = "hard";
    	HTTPAsyncTask t = new HTTPAsyncTask(this);
    	t.execute(getString(R.string.baseURL) + "/changeTempo.php?tempo="+tempo+"&roomName=" + roomName + "&studentID=" + deviceUuid);
    	ImageView imageView = (ImageView) getActivity().findViewById(R.id.bgImageView);
    	imageView.setImageResource(R.drawable.hard);
    }
    
    public void onClickOk(View v) {
    	tempo = "ok";
    	HTTPAsyncTask t = new HTTPAsyncTask(this);
    	t.execute(getString(R.string.baseURL) + "/changeTempo.php?tempo="+tempo+"&roomName=" + roomName + "&studentID=" + deviceUuid);
    	ImageView imageView = (ImageView) getActivity().findViewById(R.id.bgImageView);
    	imageView.setImageResource(R.drawable.ok);
    }
    
    public void onClickEasy(View v) {
    	tempo = "easy";
    	HTTPAsyncTask t = new HTTPAsyncTask(this);
    	t.execute(getString(R.string.baseURL) + "/changeTempo.php?tempo="+tempo+"&roomName=" + roomName + "&studentID=" + deviceUuid);
    	ImageView imageView = (ImageView) getActivity().findViewById(R.id.bgImageView);
    	imageView.setImageResource(R.drawable.easy);
    }
    
    public void onClickLeaveRoom(View v) {
    //	Toast.makeText(this, "Leaving room...", Toast.LENGTH_SHORT).show();
    //	HTTPAsyncTask t = new HTTPAsyncTask(this);
   //	t.execute(baseUrl + "/leaveRoom.php?roomName=" + roomName + "&studentID=" + deviceUuid);
    	//this.finish();
    }
    
    
   
    
	public void httpTaskCallback(String results) {
		if(results != null){
			Toast.makeText(getActivity(), "CallBack. " + results, Toast.LENGTH_SHORT).show();
			if(results.startsWith("Student left")){
				Toast.makeText(getActivity(), "You are out of here", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(getActivity(), "CallBack. Null :(", Toast.LENGTH_SHORT).show();
		}
	}

	public void onClick(View v) {
		
		int source = v.getId();
		
		if(source == R.id.stopButton){
			onClickStop(v);
		} else if(source == R.id.hardButton){
			onClickHard(v);
		} else if(source == R.id.okButton){
			onClickOk(v);
		} else if(source == R.id.easyButton) {
			onClickEasy(v);
		}
		
	}

}
